<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/2/29
 * Time: 19:20
 */
namespace IDCU\Model;
use Think\Model\MongoModel as BaseModel;

class MongoModel extends BaseModel
{
    protected $actionTranslate = array(
        'find' => '查找',
        'select' => '查找',
        'add' => '插入',
        'save' => '更新',
        'delete' => '删除',
    );

    public function handle($action,$where,$param=null,$field=null,$order=null,$page=null){
        $pk = $this->pk;
        switch($action)
        {
            case 'find':
                if(array_key_exists($pk,$where)){
                    $data = $this->field($field)->find($where[$pk]);
                }else{
                    $data = $this->field($field)->where($where)->order($order)->find();
                }
                break;
            case 'select':
                $data['data'] = $this->field($field)->where($where)->order($order)->page($page['pageIndex'],$page['pageSize'])->select();
                $data['count'] = $this->where($where)->count();
                break;
            case 'add':
                $create = $this->create($param);
                if($create){
                    $data = $field ? $this->field($field)->add() : $this->add();
                }else{
                    return $this->result('create');
                }
                break;
            case 'save': //目前数据对象缺少主键，不能进行更新操作
                $create = $this->create($param);
                if($create){
                    if(array_key_exists($pk,$param)){
                        $data = $field ? $this->field($field)->add() : $this->save();
                    }else{
                        $data = $field ? $this->field($field)->where($where)->add() : $this->where($where)->save();
                    }
                }else{
                    return $this->result('create');
                }
                break;
            case 'delete':
                if(array_key_exists($pk,$where)){
                    $data = $this->delete($where[$pk]);
                }else{
                    $data = $this->where($where)->delete();
                }
                break;
            case 'addAll':
                break;
        }
        return $this->result($action,$data);
    }

    protected function result($action,$data=''){
        $actionTranslate = $this->actionTranslate;
        if($action == 'find' || $action == 'select'){
            if($data){ //有数据
                $result['stat'] = 1;
                if($action == 'find'){
                    $result['data'] = $data;
                    $result['count'] = 1;
                }else{
                    $result['data'] = $data['data'];
                    $result['count'] = $data['count'];
                }
                $result['msg'] = '数据'.$actionTranslate[$action].'成功，共查询到'.$result['count'].'条数据';
            }elseif($data == null){ //无数据
                $result['stat'] = 1;
                $result['data'] = array();
                $result['count'] = 0;
                $result['msg'] = '数据'.$actionTranslate[$action].'成功，共查询到'.$result['count'].'条数据';
            }else{ //查询失败
                $result['stat'] = 0;
                $result['data'] = false;
                $result['count'] = null;
                $result['msg'] = '数据'.$actionTranslate[$action].'失败，原因分析：'.$this->getDbError();
            }
        }elseif($action == 'add' || $action == 'save' || $action == 'delete'){
            if($data){
                $result['stat'] = 1;
                $result['msg'] = '数据'.$actionTranslate[$action].'成功，返回值为'.$data;
            }else{
                $result['stat'] = 0;
                $result['msg'] = '数据'.$actionTranslate[$action].'失败，原因分析：'.$this->getDbError();
            }
        }elseif($action == 'create'){
            $result['stat'] = 0;
            $result['msg'] = '创建数据对象失败，原因分析：'.$this->getError();
        }
        return $result;
    }

    protected $insertFields;

    protected $updateFields;

    protected function _before_write(&$data){
        parent::_before_write($data); // TODO: Change the autogenerated stub
    }

    protected function _before_insert(&$data,$options){
        parent::_before_insert($data,$options); // TODO: Change the autogenerated stub
    }

    protected function _before_update(&$data,$options) {
        parent::_before_update($data,$options); // TODO: Change the autogenerated stub
    }

    protected function _before_delete($options) {
        parent::_before_delete($options); // TODO: Change the autogenerated stub
    }

    protected function _after_insert($data,$options){
        parent::_after_insert($data,$options); // TODO: Change the autogenerated stub
    }

    protected function _after_update($data,$options) {
        parent::_after_update($data,$options); // TODO: Change the autogenerated stub
    }

    protected function _after_delete($data,$options) {
        parent::_after_delete($data,$options); // TODO: Change the autogenerated stub
    }

    protected function _after_select(&$resultSet,$options){
        parent::_after_select($resultSet,$options); // TODO: Change the autogenerated stub
    }

    protected function _after_find(&$result, $options)
    {
        parent::_after_find($result, $options); // TODO: Change the autogenerated stub
    }

    protected function _after_db()
    {
        parent::_after_db(); // TODO: Change the autogenerated stub
    }
}